<?php
//include '../modele/database.php';
include '../controller/company_controller.php';
include '../controller/salarie_controller.php';
$error ="";

if(isset($_POST['enregistrer']))
{
    if(is_empty($_POST['name'])== false && strlen($_POST['siret']) == 14 && is_numeric($_POST['siret']) == true)
    {
        $error = 0;
        $db = new database;
        $id_insert = $db->insert("societe","nom","adresse","siret",$_POST['name'],$_POST['adresse'],$_POST['siret']);
    }
    else if(is_empty($_POST['name'])== true)$error = 1;
    else if (strlen($_POST['siret']) != 14)$error = 2;
    else if(is_numeric($_POST['siret']) == false)$error = 3;

    header("Location: http://localhost/exalog_test/views/Create_confirmation.php?error=".$error."&id_insert=".$id_insert);
}
if(isset($_POST['salarie']))
{
    header("Location: http://localhost/exalog_test/views/Create_salarie.php?id_insert=".$_GET['id_insert']);
}


if(isset($_POST['modif']))
{
    $modif = new company_controller();
    $resultat = $modif->modif_societe($_GET['id_insert'],"societe","nom",$_POST['name'],"adresse",$_POST['adresse'],"siret",$_POST['siret']);
   
    if($_GET['modif'] != "")
        header("Location: http://localhost/exalog_test/views/Create_company.php?modif=".$_GET['modif']."&id_insert=".$_GET['id_insert']);
    else 
        header("Location: http://localhost/exalog_test/views/Create_company.php?modif&id_insert=".$_GET['id_insert']);
}       

if(isset($_POST['save_salarie']))
{
    $salarie = new salarie_controller();
    $salarie->create_salarie($_GET['id'],$_POST['name_salarie'],$_POST['firstname'],$_POST['phone'],$_POST['salaire']);
    header("Location: http://localhost/exalog_test/views/Create_company.php?modif=salarie&id_insert=".$_GET['id']);
}
function is_empty($var)
{
    if($var != "")
        return false;
    else
        return true;
}
?>
